home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Taming the Mouse / HitTestText / HitTestText.cs next >
Encoding:
Text File  |  2001-01-15  |  1.2 KB  |  46 lines

  1. //------------------------------------------
  2. // HitTestText.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class HitTestText: TypeAway
  9. {
  10.      public new static void Main()
  11.      {
  12.           Application.Run(new HitTestText());
  13.      }
  14.      public HitTestText()
  15.      {
  16.           Text += " with Hit-Testing";
  17.           Cursor = Cursors.IBeam;
  18.      }
  19.      protected override void OnMouseDown(MouseEventArgs mea)
  20.      {
  21.           if (strText.Length == 0)
  22.                return;
  23.  
  24.           Graphics grfx  = CreateGraphics();
  25.           float    xPrev = 0;
  26.           int      i;
  27.  
  28.           for (i = 0; i < strText.Length; i++)
  29.           {
  30.                SizeF sizef = grfx.MeasureString(strText.Substring(0, i + 1),
  31.                                         Font, Point.Empty, 
  32.                                         StringFormat.GenericTypographic);
  33.  
  34.                if (Math.Abs(mea.X - xPrev) < Math.Abs(mea.X - sizef.Width))
  35.                     break;
  36.  
  37.                xPrev = sizef.Width;
  38.           }
  39.           iInsert = i;
  40.  
  41.           grfx.Dispose();
  42.           PositionCaret();
  43.      }
  44. }
  45.           
  46.